package com.yc.dygang;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

public class Spider {

    public static void main(String[] args) throws IOException {
        capture();
    }

    public static void capture() throws IOException {
        URL url = new URL("https://www.dygang.cc/");
        final Document document = Jsoup.parse(url, 5000);
        final Elements elements = document.select("[id^=tab1_div_] td[width='132']");
        elements.forEach(element -> {
            //element ==> border1
            // TODO 采集 id, type, name, image ,intime
            final Element a = element.selectFirst("table:nth-child(1) a");
            final String href = a.attr("href");
            final String type = href.replaceAll("/(\\w+)/.*", "$1");
            final String intime = href.replaceAll("/(\\w+)/(\\d+)/.*", "$2");
            final String id = href.replaceAll("/(\\w+)/(\\d+)/(\\d+).*", "$3");
            final Element img = element.selectFirst("table:nth-child(1) a>img");
            final String image = img.attr("src");
            final String name = img.attr("alt");
            System.out.printf("%s, %s, %s, %s, %s\n", id, name, type, intime, image);

            // TODO 使用 mybatis 保存到数据库
            // TODO 用 vue 渲染到页面
        });
    }
}
